/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.magostini.mailservice.sender;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 *
 * @author marco
 * 
 */

public class InviteSender implements Runnable{
     Logger log =Logger.getLogger("INTEGRATION-MAILSERVICE");
   


    private JavaMailSender mailSender;
    private String username;
    private String urlImageUser;
    private String email;
    private List<String>  emails;

    private VelocityEngine velocityEngine;

    public InviteSender(JavaMailSender mailSender, String username, String urlImageUser, List emails, VelocityEngine velocityEngine) {
        this.mailSender = mailSender;
        this.username = username;
        this.urlImageUser = urlImageUser;
        this.emails = emails;
        this.velocityEngine = velocityEngine;
    }


    @Override
    public void run() {

        

        for (int i = 0; i < emails.toArray().length; i++) {
                email = emails.get(i);
         MimeMessagePreparator preparatorInvite = new MimeMessagePreparator() {

                    @Override
                    public void prepare(MimeMessage mimeMessage) throws Exception {

                        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                        message.setTo(email);
                        message.setSubject("WaveWish - "+username+ " ti sta invitando ad entrare nell'onda dei desideri!");
                        message.setFrom("WaveWish@wavewish.it"); // could be parameterized...
//                        message.setFrom(new InternetAddress("noreply@wavewish.it"));
                        Map model = new HashMap();

                        model.put("username", username);
                        model.put("urlImageUser", urlImageUser);



                        String text = VelocityEngineUtils.mergeTemplateIntoString(
                                velocityEngine, "template/invite/invite.vm", model);

                        message.setText(text, true);



                    }
                };



                try{
                        mailSender.send(preparatorInvite);
                }catch(Exception e){
                    log.error("NON SENDING EMAIL FOR INVITE TO "+emails.toString()+" from : "+username,e);
                    return;
                }
                log.info("EMAIL SEND FOR INVITE TO "+emails.toString()+" from : "+username );
        }                
    }

}
